package com.alibaba.citrus.ots.application.report.ability.repository.impl;

import com.alibaba.citrus.ots.application.report.ability.repository.MonthSalesEmployeeRankReportWriteRepository;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesBasicReportSDO;
import com.epoch.app.otsapplicationreportsdk.monthsalesemployeerankreport.dto.UpdateRankNoResponse;
import com.epoch.app.otsapplicationreportsdk.monthsalesemployeerankreport.dto.UpdateResponse;
import com.epoch.app.otsapplicationreportsdk.monthsalesemployeerankreport.model.MonthSalesEmployeeRankReport;
import com.epoch.app.otsapplicationreportsdk.monthsalesemployeerankreport.service.MonthSalesEmployeeRankReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2021/11/26 21:06
 */
@Component
public class MonthSalesEmployeeBankReportWriteRepositoryImpl implements MonthSalesEmployeeRankReportWriteRepository {

    @Resource
    MonthSalesEmployeeRankReportService monthSalesEmployeeRankReportService;

    @Override
    public String add(MonthSalesBasicReportSDO sdo) {
        MonthSalesEmployeeRankReport monthSalesEmployeeRankReport = new MonthSalesEmployeeRankReport();
        BeanUtils.copyProperties(sdo,monthSalesEmployeeRankReport);
        Map m = (Map)monthSalesEmployeeRankReportService.create(monthSalesEmployeeRankReport);
        Map result = (Map)m.get("result");
        return (String)result.get("lastInsertId");
    }

    @Override
    public Integer update(MonthSalesBasicReportSDO sdo) {
        MonthSalesEmployeeRankReport monthSalesEmployeeRankReport = new MonthSalesEmployeeRankReport();
        BeanUtils.copyProperties(sdo,monthSalesEmployeeRankReport);
        UpdateResponse response = monthSalesEmployeeRankReportService.update(monthSalesEmployeeRankReport);
        return response.getCount();
    }

    @Override
    public Result addOrUpdate(MonthSalesBasicReportSDO sdo) {
        MonthSalesEmployeeRankReport monthSalesEmployeeRankReport = new MonthSalesEmployeeRankReport();
        BeanUtils.copyProperties(sdo,monthSalesEmployeeRankReport);
        UpdateResponse response = monthSalesEmployeeRankReportService.update(monthSalesEmployeeRankReport);
        if(response.getCount() == 0){
            Map m = (Map)monthSalesEmployeeRankReportService.create(monthSalesEmployeeRankReport);
            Map result = (Map)m.get("result");
            return Result.success(response.getCount(),(String)result.get("lastInsertId"));
        }
        return Result.success(response.getCount());
    }

    @Override
    public Integer updateRankNo(MonthSalesBasicReportSDO sdo) {
        MonthSalesEmployeeRankReport monthSalesEmployeeRankReport = new MonthSalesEmployeeRankReport();
        BeanUtils.copyProperties(sdo,monthSalesEmployeeRankReport);
        UpdateRankNoResponse response = monthSalesEmployeeRankReportService.updateRankNo(monthSalesEmployeeRankReport);
        return response.getCount();
    }
}
